package com.ikingtech.platform.service.system.post.service;

import com.ikingtech.framework.sdk.component.api.CompPostApi;
import com.ikingtech.framework.sdk.component.model.ComponentPost;
import com.ikingtech.framework.sdk.context.security.Me;
import com.ikingtech.framework.sdk.enums.component.PickerElementTypeEnum;
import com.ikingtech.framework.sdk.post.model.PostDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import lombok.RequiredArgsConstructor;

import java.util.*;

/**
 * @author tie yan
 */
@RequiredArgsConstructor
public abstract class AbstractCompPostService implements CompPostApi {

    private final PostService service;

    @Override
    public List<ComponentPost> listByName(String name) {
        return this.convert(Tools.Coll.convertList(this.service.listByName(name, Me.tenantCode()), entity -> Tools.Bean.copy(entity, PostDTO.class)));
    }

    private List<ComponentPost> convert(List<PostDTO> posts) {
        return Tools.Coll.convertList(posts, post -> {
            ComponentPost compPost = new ComponentPost();
            compPost.setElementId(post.getId());
            compPost.setElementName(post.getName());
            compPost.setElementType(PickerElementTypeEnum.POST);
            compPost.setTenantCode(post.getTenantCode());
            return compPost;
        });
    }
}
